<template>
  <el-dialog
    :title="title"
    v-model="visible"
    width="500px"
    append-to-body
    destroy-on-close
	@close="handleCloseDialog"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="80px"
    >
      <el-form-item label="上级类别" prop="parentId">
        <el-tree-select
          v-model="formData.parentId"
          :data="categoryTreeData"
          placeholder="请选择上级类别"
          check-strictly
          :render-after-expand="false"
          :props="{
            label: 'name',
            value: 'id',
            children: 'children'
          }"
        />
      </el-form-item>
	  <el-form-item label="类别名称" prop="name">
	    <el-input v-model="formData.name" placeholder="请输入类别名称" />
	  </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="formData.sort" :min="0" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="handleCloseDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from 'vue';
import type { FormInstance } from 'element-plus';
import { ElMessage } from 'element-plus';
import type { ParamModelCategoryVO, ParamModelCategoryForm } from '@/api/biz/parammodel';
import ParamModelAPI from '@/api/biz/parammodel';

const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true
  },
  title: {
    type: String,
    required: true
  },
  category: {
    type: Object as () => ParamModelCategoryVO,
    required: true
  },
  categoryTreeData: {
    type: Array as () => ParamModelCategoryVO[],
    required: true
  }
});

const emit = defineEmits(['update:modelValue', 'success']);

const visible = ref(false);
const formRef = ref<FormInstance>();
const formData = reactive<ParamModelCategoryForm>({
  id: '',
  name: '',
  parentId: '',
  sort: 0
});

const rules = {
  name: [{ required: true, message: '请输入类别名称', trigger: 'blur' }],
  sort: [{ required: true, message: '请输入排序', trigger: 'blur' }]
};

watch(() => props.modelValue, (val) => {
  visible.value = val;
});

watch(() => visible.value, (val) => {
  emit('update:modelValue', val);
});

watch(
  () => props.category,
  (val) => {
    Object.assign(formData, val);
  },
  { deep: true }
);

const submitForm = async () => {
  if (!formRef.value) return;
  await formRef.value.validate();
  await ParamModelAPI.saveCategory(formData);
  ElMessage.success('保存成功');
  handleCloseDialog();
  emit('success');
};


function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id="";
  formData.name="";
  formData.sort=0;
}

function handleCloseDialog() {
  visible.value = false;
  resetForm();
}

</script>

<style lang="scss" scoped>
.dialog-footer {
  text-align: right;
}
</style> 